package unit

import (
	"fmt"
	"github.com/goravel/framework/facades"
	"github.com/spf13/viper"
	"goravel/app/models"
)

type UnitService struct{}

func NewUnitService() *UnitService {
	return &UnitService{}
}

func (m *UnitService) InitUnit() error {
	return m.ParseFile()
}
func (m *UnitService) ParseFile() error {
	//先trancatemenus表
	v2 := viper.New()
	v2.SetConfigFile("./config.json")
	v2.SetConfigName("config")
	v2.SetConfigType("json")
	v2.AddConfigPath("app/services/unit") // 第一个搜索路径
	if err := v2.ReadInConfig(); err != nil {
		fmt.Printf("Error reading config file: %s\n", err)
		return err
	}
	type UnitData struct {
		Data []models.Unit `json:"data"`
	}
	var unitData UnitData
	if err := v2.Unmarshal(&unitData); err != nil {
		fmt.Println("Error unmarshalling JSON:", err)
		return err
	}
	return facades.Orm().Query().Create(&unitData.Data)
}
